/**
  ******************************************************************************
  * File Name          : weight.c
  * Description        : Code for freertos applications
  ******************************************************************************
  * @attention
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "bsp.h"
#include "tskcfg.h"
#include "ltlx.h"
#include "route_expio_485.h"
#include <string.h>

const Route_DevType_Type Route_LocalDevType = RT_DV_LX;

static void route_expio_lx_up_state(Route_PayloadType *outpl)
{
  if(memcmp((void *)&LtLx_State, &LtLx_State_last, sizeof(LtLx_StateType)) != 0)
  {
    outpl->trans_dir = ROUTE_TRSDIR_SUBDEV_TO_NET;
    outpl->pl_type   = ROUTE_PLTYP_LX_CYCLE;
    outpl->pl_len    = sizeof(LtLx_StateType);
    *((LtLx_StateType *)(outpl->payload)) = LtLx_State;
    
    LtLx_State_last = LtLx_State;
  }
  else
  {
    outpl->trans_dir = ROUTE_TRSDIR_SUBDEV_TO_LOCAL;
    outpl->pl_type   = ROUTE_PLTYP_LX_ACK;
    outpl->pl_len    = 0;
  }
}

static void route_expio_lx_up_sync(Route_PayloadType *outpl)
{
  outpl->trans_dir = ROUTE_TRSDIR_SUBDEV_TO_NET;
  outpl->pl_type   = ROUTE_PLTYP_LX_SYNC;
  outpl->pl_len    = sizeof(LtLx_SyncType);
 
  ((LtLx_SyncType *)(outpl->payload))->hour   = Sys_Runtime.hour;
  ((LtLx_SyncType *)(outpl->payload))->minute = Sys_Runtime.minute;
  ((LtLx_SyncType *)(outpl->payload))->second = Sys_Runtime.second;
  ((LtLx_SyncType *)(outpl->payload))->state  = LtLx_State;
  ((LtLx_SyncType *)(outpl->payload))->para   = LtLx_RunPara;
}

uint8_t Route_expio_xx_rxprocess(Route_PayloadType *inpl, Route_PayloadType *outpl)
{
  uint8_t sftype;
  uint8_t err = 0;
  
  switch(inpl->pl_type)
  {
    case ROUTE_PLTYP_LX_CYCLE:
      route_expio_lx_up_state(outpl);
      break;
    
    case ROUTE_PLTYP_LX_PARA:
      if(inpl->pl_len != sizeof(LtLx_SyncType))
      {
        return 1;
      }
      
      if(memcmp(&LtLx_RunPara, &(((LtLx_SyncType *)(inpl->payload))->para), sizeof(LtLx_ParaType)) != 0)
      {
        if(LtLx_ParaCheck(&(((LtLx_SyncType *)(inpl->payload))->para)) != 0)
        {
          return 3;
        }
        
        LtLx_RunPara = ((LtLx_SyncType *)(inpl->payload))->para;
        
        if(Storage_qhd != 0)
        {
          sftype = 1;
          xQueueSend(Storage_qhd, &sftype, 0);
        }
      }
      
      Sys_Adjtime.hour   = ((LtLx_SyncType *)(inpl->payload))->hour;
      Sys_Adjtime.minute = ((LtLx_SyncType *)(inpl->payload))->minute;
      Sys_Adjtime.second = ((LtLx_SyncType *)(inpl->payload))->second;
      Sys_SetDataTime();
      
      route_expio_lx_up_sync(outpl);
      break;
    
    case ROUTE_PLTYP_LX_SYNC:
      route_expio_lx_up_sync(outpl);
      break;
    
    case ROUTE_PLTYP_LX_DOACT:
      if(inpl->pl_len != sizeof(LtLx_ActType))
      {
        return 1;
      }
      
      if(LtLx_RxCmd_Qhd == NULL)
      {
        return 4;
      }
      
      if(((LtLx_ActType *)(inpl->payload))->lxkg == 1)
      {
        xQueueSend(LtLx_RxCmd_Qhd, &LtLx_SendCmdOn, 0);
      }
      else if(((LtLx_ActType *)(inpl->payload))->lxkg == 0)
      {
        xQueueSend(LtLx_RxCmd_Qhd, &LtLx_SendCmdOff, 0);
      }
      else
      {
        return 3;
      }
      
      route_expio_lx_up_state(outpl);
      break;
    
    default:
      return 2;
  }
  
  return err;
}
